﻿namespace SharePointCSVImporter.RuleEditors
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;

    public partial class ExternalLookupReplace : UserControl
    {
        #region Fields

        private ExternalLookupVM vm;
        CSVParser.FieldMapping _mapping;

        #endregion Fields

        #region Constructors

        public ExternalLookupReplace(ref CSVParser.FieldMapping mapping, List<CSVParser.FieldMapping> currentMappings)
        {
            InitializeComponent();
            _mapping = mapping;
            vm = new ExternalLookupVM(_mapping.Transformation);
            vm.AvailableColumns = currentMappings.Select(c => c.CsvColumnName).Distinct().ToList();

            this.DataContext = vm;
        }

        #endregion Constructors

        #region Properties

        public ExternalLookupVM ExternalLookupVM
        {
            get { return vm; }
            set { vm = value; }
        }

        #endregion Properties

        #region Methods

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.InstructionsPopup.IsOpen = true;
        }

        private void GetFieldsButton_Click(object sender, RoutedEventArgs e)
        {
            this.GetFieldsButton.Content = "Retrieving Fields";
            this.vm.GetExternalListFields();
        }

        private void InstructionCloseBtn_Click(object sender, RoutedEventArgs e)
        {
            this.InstructionsPopup.IsOpen = false;
        }

        #endregion Methods
    }
}